package holiday_clients;

import holiday_packagesmanager.AgencyManager;
import holiday_packages.HPackage;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Date;

/**
 *
 * @author grp02 - Pascal Charreix and Meike Paetz
 *
 * The TravelAgencyClient is the client for the TravelAgency where statements
 * for Holiday Packages are requested.
 */
public class TravelAgencyClient {

    private AgencyManager Manager;
    final String host = "rmi://127.0.0.1:1099/";

    public static void main(String argv[]) {
        HPackage[] packList;
        int i;
        int reserved = 0;

        packList = new HPackage[6];
        TravelAgencyClient client = new TravelAgencyClient();
        try {
            packList[0] = client.findByCountry("Sverige");
            if (packList[0] != null) {
                packList[0].unlock();
            }
            packList[1] = client.findByDate(new Date(), new Date(System.currentTimeMillis() + 100000));
            if (packList[1] != null) {
                packList[1].unlock();
            }
            packList[2] = client.findByHotelName("Ibis");
            if (packList[2] != null) {
                packList[2].unlock();
            }
            packList[3] = client.findByHotelStars(3);
            if (packList[3] != null) {
                packList[3].unlock();
            }
            packList[4] = client.findByPrice(1000);
            if (packList[4] != null) {
                packList[4].unlock();
            }
            packList[5] = client.findByFlightOperator("Viking Airlines");
            if (packList[5] != null) {
                packList[5].unlock();
            }
        } catch (RemoteException re) {
            System.err.println("Remote exception while unlocking package: " + re.getMessage());
        }
        i = 0;
        while (i < 6 && reserved == 0) {
            if (packList[i] != null) {
                reserved = client.reservePackage(packList[i]);
            }
            i++;
        }
        client.cancelReservation(reserved);
    }

    public TravelAgencyClient() {
        try {
            Manager = (AgencyManager) Naming.lookup(host + "grp02Manager");
        } catch (RemoteException re) {
            System.err.println("Remote exception while looking up manager: " + re.getMessage());
        } catch (NotBoundException nbe) {
            System.err.println("Server object not found at expected URL: " + nbe.getMessage());
        } catch (MalformedURLException mue) {
            System.err.println("Bad RMI URL given for server object: " + mue.getMessage());
        }
    }

    /**
     *
     * requests for a suitable Holiday Package by country
     */
    public HPackage findByCountry(String Country) {
        HPackage pack = null;
        try {
            System.out.println("Looking for packages...\tCountry like " + Country + ":");
            pack = Manager.findByCountry(Country);
            if (pack == null) {
                System.out.println("Not found.");
            } else {
                System.out.print(pack.getFullInformation());
            }
        } catch (RemoteException re) {
            System.err.println("Remote exception while finding by country : " + re.getMessage());
        }
        return pack;
    }

    /**
     *
     * requests for a suitable Holiday Package by date
     */
    public HPackage findByDate(Date Start, Date End) {
        HPackage pack = null;
        try {
            System.out.println("Looking for packages...\t Date between " + Start.toString() + " and " + End.toString() + ":");
            pack = Manager.findByDate(Start, End);
            if (pack == null) {
                System.out.println("Not found.");
            } else {
                System.out.print(pack.getFullInformation());
            }
        } catch (RemoteException re) {
            System.err.println("Remote exception while finding by date : " + re.getMessage());
        }
        return pack;
    }

    /**
     *
     * requests for a suitable Holiday Package by hotel name
     */
    public HPackage findByHotelName(String Name) {
        HPackage pack = null;
        try {
            System.out.println("Looking for packages...\tHotel name like " + Name + ":");
            pack = Manager.findByHotelName(Name);
            if (pack == null) {
                System.out.println("Not found.");
            } else {
                System.out.print(pack.getFullInformation());
            }
        } catch (RemoteException re) {
            System.err.println("Remote exception while finding by hotel name : " + re.getMessage());
        }
        return pack;
    }

    /**
     *
     * requests for a suitable Holiday Package by amount of hotel stars
     */
    public HPackage findByHotelStars(int Stars) {
        HPackage pack = null;
        try {
            System.out.println("Looking for packages...\tHotel with " + Stars + " stars:");
            pack = Manager.findByHotelStars(Stars);
            if (pack == null) {
                System.out.println("Not found.");
            } else {
                System.out.print(pack.getFullInformation());
            }
        } catch (RemoteException re) {
            System.err.println("Remote exception while finding by hotel stars : " + re.getMessage());
        }
        return pack;
    }

    /**
     *
     * requests for a suitable Holiday Package by price
     */
    public HPackage findByPrice(float Price) {
        HPackage pack = null;
        try {
            System.out.println("Looking for packages...\tPrice ~= " + Price + ":");
            pack = Manager.findByPrice(Price);
            if (pack == null) {
                System.out.println("Not found.");
            } else {
                System.out.print(pack.getFullInformation());
            }
        } catch (RemoteException re) {
            System.err.println("Remote exception while finding by price : " + re.getMessage());
        }
        return pack;
    }

    /**
     *
     * requests for a suitable Holiday Package by flight operator
     */
    public HPackage findByFlightOperator(String Name) {
        HPackage pack = null;
        try {
            System.out.println("Looking for packages...\tFlight Operator like " + Name + ":");
            pack = Manager.findByFlightOperator(Name);
            if (pack == null) {
                System.out.println("Not found.");
            } else {
                System.out.print(pack.getFullInformation());
            }
        } catch (RemoteException re) {
            System.err.println("Remote exception while finding by flight operator : " + re.getMessage());
        }
        return pack;
    }

    /**
     *
     * requests for a  Holiday Package by reservation number
     */
    public HPackage findByReservationNumber(int Reservation) {
        HPackage pack = null;
        try {
            System.out.println("Looking for packages...\tReservation Number = " + Reservation + ":");
            pack = Manager.findByReservationNumber(Reservation);
            if (pack == null) {
                System.out.println("Not found.");
            } else {
                System.out.print(pack.getFullInformation());
            }
        } catch (RemoteException re) {
            System.err.println("Remote exception while finding by reservation number : " + re.getMessage());
        }
        return pack;
    }

    /**
     *
     * reserves found suitable Holiday Package
     */
    public int reservePackage(HPackage pack) {
        int res = 0;
        try {
            System.out.println("Reserving package...");
            System.out.print(pack.getFullInformation());
            res = pack.reserve();
            System.out.println("Done !");
        } catch (RemoteException re) {
            System.err.println("Remote exception while reserving Package : " + re.getMessage());
        }
        return res;
    }

    /**
     *
     * cancels reserved Holiday Package
     */
    public void cancelReservation(int Id) {
        HPackage pack = findByReservationNumber(Id);
        try {
            System.out.println("Canceling reservation...");
            System.out.print(pack.getFullInformation());
            pack.cancel();
            System.out.println("Done !");
        } catch (RemoteException re) {
            System.err.println("Remote exception while canceling reservation : " + re.getMessage());
        }
    }
}
